package rpcsupport

import (
	"net/rpc/jsonrpc"
	"net/rpc"
	"net"
	"log"
)

func ServerRpc(host string, service interface{}) error {
	rpc.Register(service)
	listen, error := net.Listen("tcp", host)
	if error != nil {
		return error
	}
	log.Printf("Listen on : %s", host)

	for {
		accept, err := listen.Accept()
		if err != nil {
			log.Printf("accept error : %v", err)
			continue
		}
		go jsonrpc.ServeConn(accept)

	}
	return nil

}

func NewClient(host string) (*rpc.Client, error) {
	conn, error := net.Dial("tcp", host)

	if error != nil {
		return nil, error
	}
	client := jsonrpc.NewClient(conn)
	return client, nil
}
